package com.itheima.reggie.execption;


import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;
//全局异常处理器
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public R ex1(SQLIntegrityConstraintViolationException ex){
        return R.error("已存在");
    }


    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public R ex2(CustomException ex){
        return R.error(ex.getMessage());
    }

}
